package com.ruoyi.recycle.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.recycle.domain.SalesmanManagement;
import com.ruoyi.recycle.dto.StatisticsDto;
import com.ruoyi.recycle.dto.TotalShopData;
import com.ruoyi.recycle.dto.TotalShopList;
import com.ruoyi.recycle.mapper.SalesmanManagementMapper;
import com.ruoyi.recycle.service.ISalesmanManagementService;
import com.ruoyi.recycle.vo.MyBill;
import com.ruoyi.recycle.vo.OneShopStatisticsVo;
import com.ruoyi.system.api.RemoteLoginService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.form.RegisterBody;
import com.ruoyi.system.api.model.LoginUser;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务员管理Service业务层处理
 *
 * @author Wubk
 * @date 2022-05-21
 */
@Service
public class SalesmanManagementServiceImpl extends ServiceImpl<SalesmanManagementMapper, SalesmanManagement> implements ISalesmanManagementService {
    @Autowired
    private SalesmanManagementMapper salesmanManagementMapper;
    @Autowired
    private RemoteLoginService remoteLoginService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Resource
    private RemoteUserService userService;

    /**
     * 查询业务员管理
     *
     * @param id 业务员管理主键
     * @return 业务员管理
     */
    @Override
    public SalesmanManagement selectSalesmanManagementById(Long id) {
        return salesmanManagementMapper.selectSalesmanManagementById(id);
    }

    /**
     * 查询业务员管理列表
     *
     * @param salesmanManagement 业务员管理
     * @return 业务员管理
     */
    @Override
    public List<SalesmanManagement> selectSalesmanManagementList(SalesmanManagement salesmanManagement) {
        return salesmanManagementMapper.selectSalesmanManagementList(salesmanManagement);
    }

    /**
     * 新增业务员管理
     *
     * @param salesmanManagement 业务员管理
     * @return 结果
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public int insertSalesmanManagement(SalesmanManagement salesmanManagement) {
        remoteLoginService.register(new RegisterBody()
                .setUsername(salesmanManagement.getSalesmanNumber())
                .setPassword(salesmanManagement.getSalesmanPassword()));
        return salesmanManagementMapper.insert(salesmanManagement.setIdentifyStatus("未认证"));
    }

    /**
     * 修改业务员管理
     *
     * @param salesmanManagement 业务员管理
     * @return 结果
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public int updateSalesmanManagement(SalesmanManagement salesmanManagement) {
        SalesmanManagement oldSalesman = getById(salesmanManagement.getId());
        if (ObjectUtils.isNotEmpty(oldSalesman)) {

            if (oldSalesman.getSalesmanPassword().equals(salesmanManagement.getSalesmanPassword())) {
                R<LoginUser> userInfo = userService.getUserInfo(oldSalesman.getSalesmanNumber(), "admin");
                SysUser sysUser = userInfo.getData().getSysUser();
                sysUser.setPassword(salesmanManagement.getSalesmanPassword());
                userService.resetPwd(sysUser, "admin");
            }

        }

        return salesmanManagementMapper.updateById(salesmanManagement);
    }

    /**
     * 批量删除业务员管理
     *
     * @param ids 需要删除的业务员管理主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanManagementByIds(Long[] ids) {
        final List<SalesmanManagement> managements = list(Wrappers.lambdaQuery(new SalesmanManagement())
                .in(SalesmanManagement::getId, ids));
        final List<Long> userIds = managements.stream().map(SalesmanManagement::getUserId).collect(Collectors.toList());
        remoteUserService.remove(ArrayUtil.toArray(userIds, Long.TYPE), SecurityConstants.INNER);
        return salesmanManagementMapper.deleteSalesmanManagementByIds(ids);
    }

    /**
     * 删除业务员管理信息
     *
     * @param id 业务员管理主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanManagementById(Long id) {
        return salesmanManagementMapper.deleteSalesmanManagementById(id);
    }


    @Override
    public TotalShopData selectTotalShopData(Long salesmanId) {

        return salesmanManagementMapper.selectTotalShopData(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalShopList(Long salesmanId) {
        return salesmanManagementMapper.selectTotalShopList(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalShopListOrderByToDayNewClient(Long salesmanId) {
        return salesmanManagementMapper.selectTotalShopListOrderByToDayNewClient(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalShopListOrderByMonthNewClient(Long salesmanId) {
        return salesmanManagementMapper.selectTotalShopListOrderByMonthNewClient(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalShopListOrderByMonthOrder(Long salesmanId) {
        return salesmanManagementMapper.selectTotalShopListOrderByMonthOrder(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalShopListByShopType(Long salesmanId, String shopType) {
        return salesmanManagementMapper.selectTotalShopListByShopType(salesmanId, shopType);
    }

    @Override
    public List<TotalShopList> selectTotalShopListBySignTime(Long salesmanId, String signTime) {
        return salesmanManagementMapper.selectTotalShopListBySignTime(salesmanId, signTime);
    }

    @Override
    public OneShopStatisticsVo oneShopData(Long shopId) {
        return salesmanManagementMapper.oneShopData(shopId);
    }


    @Override
    public Long selectTotalPersonalInformation(Long salesmanId) {
        return salesmanManagementMapper.selectTotalPersonalInformation(salesmanId);
    }

    @Override
    public MyBill myBill(Long salesmanId) {

        return salesmanManagementMapper.myBill(salesmanId);
    }

    @Override
    public List<StatisticsDto> newClientSum(Long shopId) {
        return salesmanManagementMapper.newClientSum(shopId);
    }

    @Override
    public List<StatisticsDto> orderSum(Long shopId) {
        return salesmanManagementMapper.orderSum(shopId);
    }

    @Override
    public List<StatisticsDto> shopIncome(Long shopId) {
        return salesmanManagementMapper.shopIncome(shopId);
    }

    @Override
    public List<StatisticsDto> salesmanIncome(Long shopId) {
        return salesmanManagementMapper.salesmanIncome(shopId);
    }

    @Override
    public List<StatisticsDto> newClientSumDay(Long shopId, String signTime) {
        return salesmanManagementMapper.newClientSumDay(shopId, signTime);
    }

    @Override
    public List<StatisticsDto> orderSumDay(Long shopId, String signTime) {
        return salesmanManagementMapper.orderSumDay(shopId, signTime);
    }

    @Override
    public List<StatisticsDto> shopIncomeDay(Long shopId, String signTime) {
        return salesmanManagementMapper.shopIncomeDay(shopId, signTime);
    }

    @Override
    public List<StatisticsDto> salesmanIncomeDay(Long shopId, String signTime) {
        return salesmanManagementMapper.salesmanIncomeDay(shopId, signTime);
    }

    @Override
    public TotalShopData selectTotalClientData(Long salesmanId) {
        return salesmanManagementMapper.selectTotalClientData(salesmanId);
    }

    @Override
    public List<TotalShopList> selectTotalClientList(Long salesmanId, Integer type, String signTime) {
        return salesmanManagementMapper.selectTotalClientList(salesmanId, type, signTime);
    }

    @Override
    public OneShopStatisticsVo oneClientData(Long clientId) {
        return salesmanManagementMapper.oneClientData(clientId);
    }

    @Override
    public List<StatisticsDto> clientNewClientSumDay(Long clientId, String signTime) {
        return salesmanManagementMapper.clientNewClientSumDay(clientId, signTime);
    }

    @Override
    public List<StatisticsDto> clientOrderSumDay(Long clientId, String signTime) {
        return salesmanManagementMapper.clientOrderSumDay(clientId, signTime);
    }

    @Override
    public List<StatisticsDto> clientOrderIntegralSumDay(Long clientId, String signTime) {
        return salesmanManagementMapper.clientOrderIntegralSumDay(clientId, signTime);
    }

    @Override
    public List<StatisticsDto> clientSalesmanIncomeDay(Long clientId, String signTime) {
        return salesmanManagementMapper.clientSalesmanIncomeDay(clientId, signTime);
    }

    @Override
    public List<StatisticsDto> clientNewClientSum(Long clientId) {
        return salesmanManagementMapper.clientNewClientSum(clientId);
    }

    @Override
    public List<StatisticsDto> clientOrderSum(Long clientId) {
        return salesmanManagementMapper.clientOrderSum(clientId);
    }

    @Override
    public List<StatisticsDto> clientShopIncome(Long clientId) {
        return salesmanManagementMapper.clientShopIncome(clientId);
    }

    @Override
    public List<StatisticsDto> clientSalesmanIncome(Long clientId) {
        return salesmanManagementMapper.clientSalesmanIncome(clientId);
    }
}
